package services

import (
	"github.com/gin-gonic/gin"
	"github.com/uniplaces/carbon"
	"hitme/go-api/logic/form"
	"hitme/go-api/logic/repository"
	"hitme/go-api/models"
	"hitme/go-api/modules/api"
)

type ossFileService struct {}

var OssFileService = new(ossFileService)

// Crontab 定时处理七天后的数据
func (u *ossFileService) Crontab (c *gin.Context, req form.OssFileCrontabReqForm) (*form.OssFileCrontabRespForm, error) {
	// 查找七天前的数据
	var data []models.OssFile
	models.OssFileModel.Model().Where("created_at < ?", carbon.Now().SubDays(8).DateTimeString()).Where("deleted_at = '0000-00-00 00:00:00'").Find(&data)

	for _, val := range data {
		err := api.OssApi.DeleteObject(val.EndPoint, val.AccessKeyId, val.AccessKeySecret, val.BucketName, val.Object)
		if err != nil {
			continue
		}
		repository.OssFileRep.SetDeleted(val.Id)
	}

	return nil, nil
}